<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="details" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="100">
			</el-table-column>
			<el-table-column type="index" width="100">
			</el-table-column>
			<el-table-column prop="name" label="明细名" width="180" sortable>
			</el-table-column>
			<el-table-column prop="type.name" label="字典类型" width="180" sortable>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--修改或者新增界面-->
		<el-dialog append-to-body="true" title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="明细名" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-select v-model="editForm.type" value-key= "id" placeholder="请选择">
					<el-option
							v-for="item in types"
							:key="item.id"
							:label="item.name"
							:value="item">
						<span style="float: left">{{ item.name }}</span>
						<span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
					</el-option>
				</el-select>
				<!--<el-form-item label="上级部门">
					<el-cascader v-model="editForm.parent"
							:options="empTree"
							:props="{
								checkStrictly: true,
								label: 'name',//双引号不行
								value: 'id'
							}"
							clearable>
					</el-cascader>
				</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>


	export default {
		data() {
			return {
				filters: {
					keyword:''
				},
				types: [],
				typeId:0,
				details: [],
                /*empTree: [],*/
				total: 0,
				currentPage: 1,
                pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入明细名', trigger: 'blur' }
					],
                    type: [
                        { required: true, message: '请选择类型', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {
					id: null,
                    name: '',
                    type:null
				},

			}
		},
		methods: {

		    /*getTree(){
		        this.$http.post("/dept/tree")
                    .then(result => {
                        this.empTree = result.data;
                    })
			},*/
		    getTypes(){
                this.$http.get("/type/list")
                    .then(result => {
                        this.types = result.data;
                    })
			},

            search(){
                this.currentPage = 1;
                this.getDetails();
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDetails();
			},
			//获取部门列表
			getDetails() {
				let para = {
                    currentPage: this.currentPage,
					pageSize: this.pageSize,
					typeId : this.typeId,
					keyword: this.filters.keyword
				};
				this.listLoading = true;

                this.$http.post("/detail/page",para).then(result => {
                    this.listLoading = false;
                    this.details = result.data.rows;
                    console.log(result);
					this.total = result.data.totals;
				})

			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();

					/*removeDetails(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getDetails();
					});*/
					//发送请求
					this.$http.delete("/detail/delete/"+row.id)
						.then(result => {
                            this.listLoading = false;
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                //定位到第一页
								this.currentPage = 1;
                                this.getDetails();
							}else {
                                this.listLoading = false;
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
							}
						})
						.catch(result => {
                            this.listLoading = false;
                            this.$message({
                                message: "系统错误，正在殴打程序员",
                                type: 'error'
                            });
						})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
				/*let arr = row.dirPath.split("/");
				let tmp = [];
				for (let i = 1; i < arr.length-1; i++) {
				    tmp.push(parseInt(arr[i]));
				}
				this.editForm.parent = tmp;*/

			},
			//显示新增界面
			handleAdd: function () {
				this.editFormVisible = true;
				this.editForm = {
                    id: null,
                    name: '',
                    type:null
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							//格式化参数TODO
							/*para.parent = {id:para.parent[para.parent.length - 1]};*/
							//发送请求
							this.$http.post("/detail/save",para)
                                .then(result => {
                                    this.editLoading = false;
                                    result = result.data;
                                    if(result.success){
                                        this.$message({
                                            message: result.message,
                                            type: 'success'
                                        });
                                        //重置检验
                                        this.$refs['editForm'].resetFields();
                                        //关闭模态框
                                        this.editFormVisible = false;
                                        //刷新页面
                                        this.getDetails();
                                    }else {
                                        this.editLoading = false;
                                        this.$message({
                                            message: result.message,
                                            type: 'error'
                                        });
                                    }
                                })
                                .catch(result => {
                                    this.editLoading = false;
                                    this.$message({
                                        message: "系统错误，正在殴打程序员",
                                        type: 'error'
                                    });
                                })

							/*editDetails(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getDetails();
							});*/
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					/*batchRemoveDetails(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getDetails();
					});*/
					this.$http.patch("/detail/batch",ids)
                        .then(result => {
                            this.listLoading = false;
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                //定位到第一页
                                this.currentPage = 1;
                                this.getDetails();
                            }else {
                                this.listLoading = false;
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                        })
                        .catch(result => {
                            this.listLoading = false;
                            this.$message({
                                message: "系统错误，正在殴打程序员",
                                type: 'error'
                            });
                        })
				}).catch(() => {

				});
			},
			getSign(){
		        this.$http.get("/detail/getSign")
					.then(result => {
					    this.typeId = result.data;
                        this.getDetails();

					})
					.catch(result=>{
					    alert("wrong")
					})
			},
            getDetailsByTypeId(){

			}
		},
		mounted() {
			/*this.getDetails();*/
			/*this.getDepartments();*/
			this.getTypes();
			this.getSign();
		}
	}

</script>

<style scoped>

</style>